<?php

class Default_CommentController extends ExtLb_Controller_Action{

	private $commentTbl;
	private $postTbl;
	private $menu2Tbl;
	
    public function init()
    {
        $this->commentTbl = new Admin_Model_Comments();
        
        $this->loadCss('default', 'comment', FOLDER_ROOT_NAME);
        
        $this->postTbl		=	new Admin_Model_Posts();
        $this->menu2Tbl		= 	new Admin_Model_Menu2();
        $this->commentTbl 	= 	new Admin_Model_Comments();
        
        //RANDOM POST (BAI VIET NOI BAT)
    	$randPost = $this->postTbl->selectPost('bv.trangThai =1','baiVietId DESC',10)->query()->fetchAll();
    	$this->view->randPosts = $randPost;
    	
    	$this->view->comments = $this->commentTbl->selectComment(NULL,'yKienId DESC',5)->query()->fetchAll();

    	date_default_timezone_set('Asia/Ho_Chi_Minh');
    }

    public function indexAction()
    {
        $selectComment = $this->commentTbl->selectComment();
        $paginatorComment = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($selectComment));
        $paginatorComment->setItemCountPerPage(20)
       					->setPageRange(3)
       					->setCurrentPageNumber($this->_getParam('page',1));
       	
       	$this->view->paginatorComments = $paginatorComment;
    }
    
    public function  addAction(){
    	if (isset($_POST['cmdAdd'])){
    		try {
    			$hoTen 		= ucwords($_POST['txtName']);
	    		$chuDe 		= ucwords($_POST['txtSubject']);
	    		$email 		= $_POST['txtEmail'];
	    		$noiDung	= ucfirst($_POST['areaContent']);
	    		$now = getdate();
	        	$ngayYKien = $now['mday']."/".$now['mon']."/".$now['year'];
	        	
	    		$this->commentTbl->addComment($hoTen, $email, $chuDe, $noiDung, $ngayYKien);
	    		$this->jDialogMessage("Thông báo","Cảm ơn bạn vì đã đóng góp ý kiến");	
    		} catch (Exception $e) {
    			$this->jDialogMessage("Lỗi", "Đã có lỗi xảy ra trong quá trình xử lý. Hãy thử lại!!!");
    		}
    		
    		$this->_forward('index','index','default');
    		
    	}else {
    		$this->_redirect('default/index');
    	}
    }
    
    public function viewAction(){
    	if (isset($_GET['id']) && !empty($_GET['id'])){
    		$this->view->comment = $this->commentTbl->fetchComment("yKienId =".$_GET['id']);
    		
    		$selectComment  = $this->commentTbl->selectComment(null,'rand()',10)->query()->fetchAll();
    		
    		$this->view->commentRelated = $selectComment;
    						
    		
    	}else {
    		$this->_redirect('/default/comment/');
    	}
    }


}

